home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gekkan Dennou Club 143
/
Gekkan Dennou Club - 2000.4 Vol. 143 (Japan).7z
/
Gekkan Dennou Club - 2000.4 Vol. 143 (Japan).bin
/
docs
/
perl
/
keisyou.pl
next >
Wrap
Perl Script
|
2000-02-26
|
834b
|
58 lines
#
# 継承の例題
#
package Foo;
# インスタンスの生成
sub new {
my ($type, $a, $b) = @_;
my $obj = { 'a' => $a, 'b' => $b };
bless $obj, $type;
$obj;
}
# アクセスメソッド
sub get_a {
my $obj = shift;
$obj->{'a'};
}
# アクセスメソッド
sub get_b {
my $obj = shift;
return $obj->{'b'};
}
package Bar;
@ISA = ('Foo'); # Foo を継承
# インスタンスの生成
sub new {
my ($type, $a, $b, $c) = @_;
my $obj = $type->Foo::new( $a, $b );
# SUPER は機能しない
# my $obj = $type->SUPER::new( $a, $b );
$obj->{'c'} = $c;
$obj;
}
# アクセスメソッド
sub get_c {
my $obj = shift;
return $obj->{'c'};
}
# test
package main;
$o1 = Foo->new( 1, 2 );
$o2 = Bar->new( 10, 20, 30 );
print $o1->get_a(), "\n";
print $o2->get_a(), "\n";
print $o2->get_c(), "\n";